<%@ page pageEncoding="UTF-8"%>
<%@ include file="../commons/head.jsp"%>
<title>JS的简单运算：加减乘除</title>
</head>
<body>
<p>整型:输入框中任意数字取整位数</p>
<input type="number" id="arithmetic-num1">
<input type="number" id="arithmetic-num2">
<button onclick="checkResult1();">结果</button>
<p>浮点型：由于js浮点型的精度是17位，十进制的数，电脑以二进制运算，<br />
转化过程中会有损失，可以先全转化成整型处理，或自己写方法处理</p>
<input type="number" id="arithmetic-num3">
<select id="arithmetic-operator">
<option>--请选择--</option>
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
<option>%</option>
</select>
<input type="number" id="arithmetic-num4">
<button onclick="checkResult2();">验证</button>
<p>较明显的比如</p>
<p>0.1+0.2和0.15+0.15比较<button onclick="checkResult3();">验证</button></p>
<p>0.7+0.1和0.6+0.2比较<button onclick="checkResult4();">验证</button></p>
<script type="text/javascript">
	function checkResult1(){
		var a = parseInt($("#arithmetic-num1").val());
		var b = parseInt($("#arithmetic-num2").val());
	 	alert("和=" + (a+b));
		alert("差=" + (a-b));
		alert("乘=" + (a*b));
		alert("除=" + (a/b));
		alert("取余=" + (a%b)); 
	}
	function checkResult2(){
		var a = parseFloat($("#arithmetic-num3").val());
		var b = parseFloat($("#arithmetic-num4").val());
		var operator = $("#arithmetic-operator").val();
		var s = a + operator + b ;
		alert(eval(s));
	}
	function checkResult3(){
		alert("0.1+0.2="+(0.1+0.2));
		alert("0.15+0.15="+(0.15+0.15));
		alert("验证0.1+0.2==0.15+0.15：      "+((0.1+0.2)==(0.15+0.15)));
		
	}
	function checkResult4(){
		alert("0.7+0.1="+(0.7+0.1));//输出0.7999999999999999
		alert("0.6+0.2="+(0.6+0.2));//输出0.8
		alert("验证0.7+0.1==0.6+0.2：      "+((0.7+0.1)==(0.6+0.2)));
		
	}
</script>
</body>
</html>